import 'dart:convert';
import 'package:dio/dio.dart';

class SDioInterceptor extends Interceptor {

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
    /// 公共参数
    Map<String, dynamic> params = {
      "app_key": "mh_349b2ffe890d58e7",
      "timestamp": DateTime.now().millisecondsSinceEpoch,
      "v": 1.0,
      "sign_method": "MD5",
      "sign": "123",
    };

    if ( options.path.contains('upload')) {
      options.contentType = "application/x-www-form-urlencoded";
    }

    if (options.method == 'POST') {
      if (options.data is Map) {
        Map<String, dynamic> dataMap = jsonDecode(jsonEncode(options.data));
        dataMap.addAll(params);
        options.data = dataMap;
      }
    } else if (options.method == 'GET') {
      // options.queryParameters.addAll(params);
      Map<String, dynamic> dataMap = options.queryParameters;
      dataMap.addAll(params);
      options.data = dataMap;
    }
    super.onRequest(options, handler);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {

    super.onResponse(response, handler);
  }

  @override
  void onError(DioException err, ErrorInterceptorHandler handler) {
    super.onError(err, handler);
  }


}